package com.cl.mypureframe.fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cl.mypureframe.R;
import com.cl.mypureframe.util.UtilToast;
import com.cl.mypureframe.util.Utils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.List;

/**
 * @Class: BaseListFragment
 * @Description:
 * @Author: ChenLei
 * @Date: 2021/7/27---15:07
 */
@SuppressWarnings("rawtypes")
public abstract class BaseListFragment extends BaseFragment {
    protected int totalPage;// 总页数
    protected int currentIndex = 1;// 当前页数
    protected int loadType;// 0:下拉刷新；1：上滑加载
    protected boolean hasNextPage;// 是否有后一页
    protected RefreshLayout refreshLayout;
    protected BaseQuickAdapter myAdapter;
    protected RecyclerView rv_main;

    protected void initRecyclerViewNoReresh() {
        rv_main = rootView.findViewById(R.id.rv_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rv_main.setLayoutManager(linearLayoutManager);

        rv_main.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(this::onRvItemClick);

        execute();
    }

    protected void initRecyclerView() {
        rv_main = rootView.findViewById(R.id.rv_main);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rv_main.setLayoutManager(linearLayoutManager);

        rv_main.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(this::onRvItemClick);

        refreshLayout = rootView.findViewById(R.id.refreshLayout);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            // 下拉刷新
            loadType = 0;
            currentIndex = 0;
            // 走接口
            execute();
        });
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            // 上滑加载
            if (hasNextPage) {
                loadType = 1;
                currentIndex++;
                // 走接口
                execute();
            } else {
                refreshlayout.finishLoadMore();
                refreshlayout.setEnableLoadMore(false);
                UtilToast.show(R.string.to_alloaded);
            }
        });

        new Handler(Looper.myLooper(), msg -> {
            execute();
            return false;
        }).sendEmptyMessageDelayed(0, 300);
    }

    // 检查是否开启上滑加载
    protected void checkLoadmore() {
        refreshLayout.setEnableLoadMore(hasNextPage);
    }

    protected void onErrorCodeAction(String toast) {
        UtilToast.show(toast);
        emptyViewAndRefresh();
    }

    protected void onErrorAction() {
        emptyViewAndRefresh();
    }

    protected void onEndAction() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @SuppressWarnings("unchecked")
    protected void onSuccessAction(List list) {
        if (Utils.isListNotEmpty(list)) {
            if (loadType == 0) {// 下拉刷新
                checkLoadmore();
                myAdapter.setNewInstance(list);
            } else {// 上滑加载
                myAdapter.addData(list);
            }
        } else {
            myAdapter.setEmptyView(R.layout.view_empty_list);
        }
    }

    public abstract void onRvItemClick(BaseQuickAdapter adapter, View view, int position);

    @Override
    protected int layoutId() {
        return 0;
    }

    @SuppressWarnings("unchecked")
    protected void emptyViewAndRefresh() {
        myAdapter.setNewInstance(null);
        myAdapter.setEmptyView(R.layout.view_empty_list);
        FrameLayout frameLayout = myAdapter.getEmptyLayout();
        assert frameLayout != null;
        View v = frameLayout.findViewById(R.id.tv_reload);
        v.setOnClickListener(v1 -> execute());
    }
}
